package com.hsd.voice_asr.socket;


import com.alibaba.idst.nls.realtime.NlsClient;
import com.alibaba.idst.nls.realtime.NlsFuture;
import com.alibaba.idst.nls.realtime.event.NlsEvent;
import com.alibaba.idst.nls.realtime.event.NlsListener;
import com.alibaba.idst.nls.realtime.protocol.NlsRequest;
import com.alibaba.idst.nls.realtime.protocol.NlsResponse;
import com.hsd.voice_asr.enums.LanguageEnum;
import com.hsd.voice_asr.properties.AliAsrProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Date;

@Data
@Slf4j
public class RealtimeAsr implements NlsListener {


    private static final String asrSC = "pcm";
    private String appKey;
    private String ak_id;
    private String ak_secret;

    private NlsClient client;
    private NlsRequest req;
    private NlsFuture future;
    private Integer language;


    private Integer sentenceId = 0;
    private String text;
    private Date endTime;
    private Date startTime;

    public RealtimeAsr(Integer language, AliAsrProperties aliAsrProperties) {
        this.language = language;
        this.appKey=aliAsrProperties.getAppKey();
        this.ak_id=aliAsrProperties.getAk_id();
        this.ak_secret=aliAsrProperties.getAk_secret();
        this.client = new NlsClient();
        this.client.init();
        this.buildRequest();
    }

    public void shutDown() {
        this.client.close();
    }

    public void process(InputStream ins) {
        try {
            byte[] b = new byte[8000];
            int len;
            while ((len = ins.read(b)) > 0) {
                future.sendVoice(b, 0, len);
            }
        } catch (Exception e) {
        } finally {
            try {
                future.sendFinishSignal();
                this.client.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void buildRequest() {
        try {
            this.req = new NlsRequest();
            if (this.language == LanguageEnum.ENGLISH.getCode()) {
                appKey = "nls-service-en";
            }
            req.setAppkey(appKey);
            req.setFormat(asrSC);
            req.setResponseMode("streaming");
            req.setSampleRate(16000);
            req.authorize(ak_id, ak_secret);
            this.future = client.createNlsFuture(req, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessageReceived(NlsEvent e) {
        NlsResponse response = e.getResponse();
        if (response.result != null) {
            processResult(response);
        }
    }

    public void processResult(NlsResponse response) {
        if(response.getFinish().equals("0")&&response.getResult().getStatus_code()==0){
            text = response.getText();
            sentenceId = response.getSentence_id();
            //todo  处理接收到的文本
            log.info("receive asr text:{}",text);
            startTime = new Date();
            endTime = new Date();
        }else{
            endTime = new Date();
        }
    }

    @Override
    public void onOperationFailed(NlsEvent e) {

    }

    @Override
    public void onChannelClosed(NlsEvent e) {
    }


}
